import { CanActivate, ExecutionContext, HttpException, Injectable } from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import { Observable } from "rxjs";

export const AmdinRoleGuard = (access: string[]): CanActivate => {
	@Injectable()
	class Guard implements CanActivate {
		canActivate(context: ExecutionContext): boolean {
			let req = context.switchToHttp().getRequest();
			if (req.user["userType"] == "0") {
				return true
			}else{
				if(req.user.access.some(item => access.includes(item))){
					return true
				}else{
					throw new HttpException({
						code:1,
						message:'无权限操作，请联系管理员'
					},200)
				}
			}
		}
	}

	return new Guard();
};

